﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace Project
{
    public partial class studentProfile_ : System.Web.UI.Page
    {
        SqlConnection connection = new SqlConnection(@"Data Source=RV;Initial Catalog=StudentAdvisor;Integrated Security=True");
        SqlCommand command_profile = null;
        SqlCommand command_update = null;
        SqlDataReader dataReader = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["userID"] == null)
                Response.Redirect("Default.aspx");
            else
                lbl_username.Text = "Welcome, " + Session["userID"].ToString();

            string profile_string = "select * from tblStudentInfo where StudentId='" + Session["userID"].ToString() + "'";
            command_profile = new SqlCommand(profile_string, connection);
            try
            {
                connection.Open();
                dataReader = command_profile.ExecuteReader();
                dataReader.Read();
                txt_firstName.Text = dataReader["StudentFirstName"].ToString();
                txt_lastName.Text = dataReader["StudentLastName"].ToString();
                txt_programCode.Text = dataReader["ProgramCode"].ToString();
                txt_email.Text = dataReader["EmailPrimary"].ToString();
                txt_emailS.Text = dataReader["EmailSecondary"].ToString();
                txt_cellNumber.Text = dataReader["CellNumber"].ToString();
                //dl_international.SelectedValue = dataReader[""].ToString();

            }
            catch (SqlException ex)
            {
            }
            finally
            {
                if (!dataReader.IsClosed)
                {
                    dataReader.Close();
                    connection.Close();
                }
            }
            txt_firstName.Enabled = false;
            txt_lastName.Enabled = false;
            txt_programCode.Enabled = false;
            txt_email.Enabled = false;
            txt_emailS.Enabled = false;
            txt_cellNumber.Enabled = false;
            dl_international.Enabled = false;

        }

        protected void btn_logout_Click(object sender, EventArgs e)
        {
            Session["userID"] = null;
            Session["userType"] = null;
            Response.Redirect("Default.aspx");
        }

        protected void btn_edit_update_Click(object sender, EventArgs e)
        {
            if (btn_edit_update.Text == "Edit")
            {
                txt_firstName.Enabled = true;
                txt_lastName.Enabled = true;
                txt_programCode.Enabled = true;
                txt_email.Enabled = true;
                txt_emailS.Enabled = true;
                txt_cellNumber.Enabled = true;
                dl_international.Enabled = true;
                btn_edit_update.Text = "Update";
            }
            else
            {
                string update_string = "update tblStudentInfo set StudentFirstName='" + txt_firstName.Text + "',StudentLastName='" + txt_lastName.Text + "',ProgramCode='" + txt_programCode.Text + "',EmailPrimary='" + txt_email.Text + "',EmailSecondary='" + txt_emailS.Text + "',CellNumber='" + txt_cellNumber.Text + "',International='" + dl_international.SelectedValue + "' where StudentId='" + Session["userID"].ToString() + "'";
                command_update = new SqlCommand(update_string, connection);

                try
                {
                    connection.Open();
                    command_update.ExecuteNonQuery();
                    connection.Close();
                    lbl_update.Text = "Your profile has been updated...";
                }
                catch (SqlException ex)
                {
                    lbl_update.Text = ex.ToString();
                }
                finally
                {

                }
                btn_edit_update.Text = "Edit";
            }
        }
    }
}